<?php


namespace Admin\Controller;
use Think\Controller;

class TransactionController  extends BaseController
{
    public function getlist(){

        $auth = auth_check(C('auth_code'),$_SERVER['HTTP_HOST']);
        if (!$auth) {
            echo "未授权或授权已过期";exit;
        }

        $nickname = I('nickname');
        $db=M('transaction');
        if($nickname){
            $map['username'] = array('like',"%$nickname%");
            $map['type']=1;
            $count =$db->where($map)->count();
            $page = new \Think\Page($count,10);
            $show = $page->show();
            $list = M('transaction')->where($map)->limit($page->firstRow.','.$page->listRows)->order("id desc")->select();
        }else{
            $where=array(
                'type'=>1
            );
            $count = $db->field("sum(zcmoney) as sum_points,count(id) as count")->where($where)->find();
            $page = new \Think\Page($count['count'],10);
            $show = $page->show();
            $list=  $db->where($where)->limit($page->firstRow.','.$page->listRows)->order("id desc")->select();
        }
        $this->assign('show',$show);
        $this->assign('nickname',$nickname);
        $this->assign('list',$list);
        $this->display();
    }
    public  function set_robot(){
       $id= I('id');
        //确认逻辑
        $db=M('transaction');
        $udb=M('user');
        //查找是否存在订单
        $tdata=$db->where("id={$id}")->find();

        //更改转出人金额
        $zcuserinfo=$udb->where("id={$tdata['userid']}")->find();
        if($zcuserinfo){
            $zcuserdata=array(
                'points'=>$zcuserinfo['points']-$tdata['zcmoney']
            );
            if($zcuserinfo['points']<$tdata['zcmoney']){
                $this->error('转出余额不足,请确认');
            }
            $zcdata=$udb->where("id={$tdata['userid']}")->setField($zcuserdata);
            if($zcdata){
                //更改转入人金额
                $zruserinfo=$udb->where("id={$tdata['zruserid']}")->find();
                if($zruserinfo){
                    $zruserdata=array(
                        'points'=>$zruserinfo['points']+$tdata['zrmoney']
                    );
                    $zrdata=$udb->where("id={$tdata['zruserid']}")->setField($zruserdata);
                    if($zrdata){
                        if($tdata){
                            //修改订单状态
                            $tdatas=array(
                                'status'=>1
                            );
                            $db->where("id={$id}")->setField($tdatas);//转出状态
                            $db->where("orderid={$id}")->setField($tdatas);//转入状态
                            $this->success('确认成功');
                        }else{
                            $this->error('确认失败');
                        }
                    }else{
                        $this->error('确认失败');
                    }
                }else{
                    $this->error('确认失败');
                }
            }
        }
    }

    public function del_robot(){
        $id= I('id');
        $db=M('transaction');
        //查找是否存在订单
        $tdata=$db->where("id={$id}")->find();
        if($tdata){
            //修改订单状态
            $tdatas=array(
                'status'=>2
            );
            $db->where("id={$id}")->setField($tdatas);//转出状态
            $db->where("orderid={$id}")->setField($tdatas);//转入状态
            $this->success('取消成功');
        }else{
            $this->error('取消失败');
        }
    }
}